package com.zhiyinlou.api.testcase.base;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.TreeMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;

import com.zhiyinlou.api.util.file.ParsePropertiesUtil;
import com.zhiyinlou.api.util.file.ParseXLSXUtil;


public class BaseCase {
	protected static Logger logger = LogManager.getLogger(BaseCase.class);
	// 配置文件config.xml
	public static Properties prop = ParsePropertiesUtil.getInstance();
	// 测试数据文件，保存在TestData下
	public String testfile;
	public static List<List<String>> lst = new ArrayList<List<String>>();
	
	// 保存测试结果的map
	public static TreeMap<Long, Integer> resultsMap;
	

	public BaseCase() {
		logger.info("[case]初始化类:" + this.getClass().getName());
		// 测试数据文件，文件名字必须和testcase的类名相同
		testfile = "TestData" + File.separator + this.getClass().getName().replace(".", File.separator) + ".xlsx";
		resultsMap = new TreeMap<Long, Integer>();
	}

	// 测试数据保存到二维数组testdata，sheet的名字必须和testng的测试方法相同都为test
	@DataProvider(name = "dataSource")
	public Object[][] dataSource(Method method) {
		logger.info("[BaseCase]开始准备测试数据...");
		Object[][] testdata = null;
		// 测试方法默认为test
		testdata = ParseXLSXUtil.getDataValue(testfile, "test");		
		return testdata;
	}

	@BeforeSuite
	public void init() {
		logger.info("[BaseCase]暂时保留...");
		
	}

	@BeforeClass
	public void init_fields() {
		logger.info("================================================================");
		logger.info("[BaseCase]开始执行case: " + this.getClass().getName());
		logger.info("[BaseCase]初始化business...");
		
	
	}
	
	@AfterMethod
	public void addData() {	
		
		
	}
	@AfterClass
	public void after_class() {
		logger.info("[BaseCase]暂时保留..");	
		// 保存测试结果
		logger.info("保存测试结果...");
		if (testfile != null && !resultsMap.isEmpty()) {
			ParseXLSXUtil.saveResults(testfile, resultsMap);
		}
	}


	@AfterSuite
	public void saveCSV() {
		


	}
}
